var Post = require('../handlers/postHandler')
var Comment = require('../handlers/commentHandler')
var util = require('../utility/util')

module.exports = {

  add: function (req, res) {
    var id = req.params.id
    var body = req.body
    var commentId = body.commentId
    var comment = util.createCommentDb(body.comment)

    Post.findById(id, function (err, post) {
      if (err) {
        console.log('commentController add fail. err = ' + err)
        return res.json({code: 177, msg: err})
      }

      Comment.insert({
        userId: post.handler.id,
        postId: id,
        commentId: commentId
      }, comment, function (err ,newPost) {
        if (err) {
          console.log('commentController add fail. err = ' + err)
          return res.json({code: 177, msg: err})
        }
        return res.json({code: 100, msg: '评论成功', data: {post: newPost}})
      })
    })
  }

}